/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-13 14:36:25
 * @LastEditTime: 2019-09-13 18:01:35
 * @LastEditors: Please set LastEditors
 */

const JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require("mongoose");
const User = mongoose.model("users");
const keys = require("../config/keys");

const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;
module.exports = passport => {
    passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
      User.findById(jwt_payload.id)
          .then(user => {
            if(user){
                console.log(user)
              return done(null,user);
            }
  
            return done(null,false);
          })
          .catch(err => console.log(err));
    }));
  }